<template>
	<view :data-theme="theme">
		<uni-nav-bar title="评论列表" left-icon="back" @clickLeft="handleBack" :border="false" :fixed="true"
			:status-bar="true" :background-color="navBg"></uni-nav-bar>
		<view style="height: 100%;">
			<view class='evaluate-list'>
				<view class='generalComment acea-row row-between-wrapper'>
					<view class='acea-row row-middle font_color'>
						<view class='evaluate'>评分</view>
						<view class='start'
							:class="'star'+ (replyData.replyStar)">
						</view>
					</view>
					<view><text class='font_color'>{{replyData.replyChance ? (replyData.replyChance)*100 : 0}}%</text>好评率</view>
				</view>
				<view class='nav acea-row row-middle'>
					<view class='item' :class='type==0 ? "bg-color":""' @click='changeType(0)'>全部({{replyData.sumCount || 0}})
					</view>
					<view class='item' :class='type==1 ? "bg-color":""' @click='changeType(1)'>好评({{replyData.goodCount || 0}})
					</view>
					<view class='item' :class='type==2 ? "bg-color":""' @click='changeType(2)'>中评({{replyData.inCount || 0}})
					</view>
					<view class='item' :class='type==3 ? "bg-color":""' @click='changeType(3)'>差评({{replyData.poorCount || 0}})
					</view>
				</view>
				<userEvaluation :reply="reply"></userEvaluation>
			</view>
			<view class='loadingicon acea-row row-center-wrapper'>
				<text class='loading iconfont icon-jiazai' :hidden='loading==false'></text>{{loadTitle}}
			</view>
			<view class='noCommodity' v-if="(type==0 && !replyData.sumCount) || (type==1 && !replyData.goodCount) || (type==2 && !replyData.inCount) || (type==3 && !replyData.poorCount)">
				<view class='pictrue'>
          <!-- <image :src="urlDomain+'crmebimage/presets/noEvaluate.png'"></image> -->
					<view class="default_txt">暂无任何评论哦~</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	
	import userEvaluation from './userEvaluation';
	let app = getApp();
	export default {
		components: {
			userEvaluation
		},
		data() {
			return {
        // urlDomain: this.$Cache.get("imgHost"),
				replyData: {},
				product_id: 0,
				reply: [],
				type: 0,
				loading: false,
				loadend: false,
				loadTitle: '加载更多',
				page: 1,
				limit: 20,
				theme:app.globalData.theme,
			};
		},
		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad: function(options) {
			console.log("🚀🚀🚀 ~ options:", options)
			if (!options.id) return
			this.productId = options.id;
		},
		onShow: function() {
			this.getProductReplyCount();
			this.getProductReplyList();
		},
		methods: {
			/**
			 * 获取评论统计数据
			 *
			 */
			getProductReplyCount:async function () {
				const reply = await this.$Api.index.replyConfig(this.productId)
				this.$set(this, 'replyData', reply.data);
				
			},
			/**
			 * 分页获取评论
			 */
			getProductReplyList: async function() {
				if (this.loadend) return;
				if (this.loading) return;
				this.loading = true;
				this.loadTitle = '';
				const params =  {
					pageNum :this.page,
					pageSize :this.limit,
					type: this.type,
				}
				try {
					const res = await this.$Api.index.replyList(this.productId, {
						...params,
						page: this.page // 确保传递当前页码
					});
					// 1. 定义新数据列表
					const newList = res.rows || [];
					// 2. 合并新旧数据（需确保 SplitArray 逻辑正确）
					this.SplitArray(newList, this.reply);
					console.log("🚀🚀🚀 ~ getProductReplyList:function ~ this.reply:", this.reply)
					// 3. 判断是否加载完毕
					const isLoadEnd = newList.length < this.limit;
					// 4. 更新状态
					this.loading = false;
					this.loadend = isLoadEnd;
					// 5. 设置加载提示文本
					this.loadTitle = isLoadEnd ? "" : "加载更多";
					// 6. 仅在成功时递增页码
					if (!isLoadEnd) this.page += 1;
				} catch (e) {
					// 7. 错误处理
					this.loading = false;
					this.loadTitle = '加载失败，点击重试'; // 建议恢复提示
					console.error('加载回复列表失败:', e);
				}
			},
			/*
			* 合并数组
			*/
			SplitArray(list, sp) {
				if (typeof list != 'object') return [];
				if (sp === undefined) sp = [];
				for (var i = 0; i < list.length; i++) {
					sp.push(list[i]);
				}
				return sp;
			},
			/*
			 * 点击事件切换
			 * */
			changeType: function(e) {
				let type = parseInt(e);
				if (type == this.type) return;
				this.type = type;
				this.page = 1;
				this.loadend = false;
				this.$set(this, 'reply', []);
				this.getProductReplyList();
			}
		},
		/**
		 * 页面上拉触底事件的处理函数
		 */
		onReachBottom: function() {
			this.getProductReplyList();
		},
	}
</script>

<style lang="scss" scoped>
	page {
		background-color: #fff;
		height: 100%;
	}
	.font_color{
		// @include main_color(theme);
	}
    .evaluate-list{
		padding: 30rpx 0 0 0;
		background-color: #fff;
	}
	.evaluate-list .generalComment {
		padding: 0 30rpx;
		margin-top: 1rpx;
		background-color: #fff;
		font-size: 28rpx;
		color: #808080;
	}

	.evaluate-list .generalComment .evaluate {
		margin-right: 7rpx;
		color: #333333;
		font-size: 28rpx;
	}

	.evaluate-list .nav {
		font-size: 24rpx;
		color: #282828;
		padding: 30rpx;
		background-color: #fff;
		border-bottom: 1rpx solid #f5f5f5;
	}

	.evaluate-list .nav .item {
		font-size: 24rpx;
		color: #282828;
		border-radius: 27rpx;
		height: 54rpx;
		padding: 0 20rpx;
		background-color: #f4f4f4;
		line-height: 54rpx;
		margin-right: 17rpx;
	}
	.acea-row{
		display: flex;
	}

	.row-between-wrapper{
		justify-content: space-between;
	}

	.evaluate-list .nav .item.bg-color {
		color: #fff;
		background-color: #ff9900;
		// @include main_bg_color(theme);
	}
	.noCommodity {
		margin-top: 32%;
	}
	.default_txt {
		font-size: 26rpx;
		color: #999;
		text-align: center;
	}
</style>
